<?php

namespace yak\layui\controllers;

use Yii;
use yak\layui\models\Dictionary;
use yak\layui\search\DictionarySearch;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;

/**
* DictionaryController implements the CRUD actions for Dictionary model.
*/
class DictionaryController extends \yak\layui\controllers\BaseController
{
	/**
	* Lists all Dictionary models.
	* @return mixed
	*/
	public function actionIndex()
	{
		$searchModel = new DictionarySearch();
		$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

		return $this->render('index', [
			'searchModel' => $searchModel,
			'dataProvider' => $dataProvider,
		]);
	}

	/**
	* Displays a single Dictionary model.
	* @param string $sign
	* @return mixed
	* @throws NotFoundHttpException if the model cannot be found
	*/
	public function actionView($sign)
	{
		return $this->renderPartial('_view', [
			'model' => $this->findModel($sign),
		]);
	}

	/**
	* Creates a new Dictionary model.
	* If creation is successful, the browser will be redirected to the 'view' page.
	* @return mixed
	*/
	public function actionCreate()
	{
		$this->layout = 'blank';
		$model = new Dictionary();
		
		return $this->render('_form', [
			'model' => $model, 
			'ajaxOptions' => [
                'modelClass' => $this->ajaxHeaderClass($model),
            ]
		]);
	}

	/**
	* Updates an existing Dictionary model.
	* If update is successful, the browser will be redirected to the 'view' page.
	* @param string $id
	* @return mixed
	* @throws NotFoundHttpException if the model cannot be found
	*/
	public function actionUpdate($sign)
	{
		$this->layout = 'blank';
		$model = $this->findModel($sign);
	
		return $this->render('_form', [
			'model' => $model,
			'ajaxOptions' => [
                'modelClass' => $this->ajaxHeaderClass($model),
                'condition' => ['sign' => $sign]
            ]
		]);
	}

	/**
	* Deletes an existing Dictionary model.
	* If deletion is successful, the browser will be redirected to the 'index' page.
	* @param string $sign
	* @return mixed
	* @throws NotFoundHttpException if the model cannot be found
	*/
	public function actionDelete($sign)
	{
		$this->findModel($sign)->delete();

		return $this->ajaxSuccess();
	}

	/**
	* Deletes an existing Dictionary model.
	* If deletion is successful, the browser will be redirected to the 'index' page.
	* @param string $id
	* @return mixed
	* @throws NotFoundHttpException if the model cannot be found
	*/
	public function actionBatchDelete()
	{
		$data = Yii::$app->request->post('Data', []);
        $ids = ArrayHelper::getValue($data, 'ids', []);
		Dictionary::deleteAll(['sign' => $ids]);

		return $this->ajaxSuccess();
	}
	
	/**
     * Output Excel.
     */
    public function actionOutput()
    {
    }

	/**
	* Finds the Dictionary model based on its primary key value.
	* If the model is not found, a 404 HTTP exception will be thrown.
	* @param string $id
	* @return Dictionary the loaded model
	* @throws NotFoundHttpException if the model cannot be found
	*/
	protected function findModel($id)
	{
		if (($model = Dictionary::findOne($id)) !== null) {
			return $model;
		}

		throw new NotFoundHttpException('The requested page does not exist.');
	}
}
